// orderResult.js
import { Util } from '../../utils/util';
import { CsDouble } from './csDouble';
import { CsFundDouble } from './csFundDouble';
import { DocBase } from './docBase';
import { DocType, CommonOrderStatus, OrderResultCategoryItemType, OrderResultCategoryType } from './docDefinition';


/// 子信息变更
export class OrderResultCategoryItem {
    constructor({
        itemId,// 子信息ID
        itemName,// 子信息名称
        type,    // type 子类别 OrderResultCategoryItemType 
        countBefore = 0, // 变更前数量
        countAfter = 0, // 变更后数量
        count = 0 // 变更数量
    }) {
        this.type = type;
        this.itemName = itemName;
        this.itemId = itemId;
        this._countBefore = countBefore;
        this._countAfter = countAfter;
        this._count = count;
    }

    _formatValue(value) {
        const fundValue = new CsFundDouble(value).value;
        const countValue = new CsDouble(value).value;

        switch (this.type) {
            case OrderResultCategoryItemType.FundAccount:
            case OrderResultCategoryItemType.ReceiveDebt:
            case OrderResultCategoryItemType.PaymentDebt:
            case OrderResultCategoryItemType.PreReceiveAccount:
            case OrderResultCategoryItemType.PrePaymentAccount:
                return fundValue;
            case OrderResultCategoryItemType.CommodityInventory:
                return countValue;
            default:
                return value;
        }
    }

    /// 变更前数量 
    get countBefore() {
        return this._formatValue(this._countBefore);
    }

    /// 变更后数量
    get countAfter() {
        return this._formatValue(this._countAfter);
    }

    /// 变更数量
    get count() {
        return this._formatValue(this._count);
    }

    static fromJson(json) {
        return new OrderResultCategoryItem({
            itemId: Util.jsonToString(json['itemId']),
            itemName: Util.jsonToString(json['itemName']),
            type: OrderResultCategoryItemType.fromCode(json['typeCode']),
            countBefore: Util.jsonToDouble(json['countBefore'], 0),
            countAfter: Util.jsonToDouble(json['countAfter'], 0),
            count: Util.jsonToDouble(json['count'], 0)
        });
    }
}

/// 类别信息变更
export class OrderResultCategory {
    constructor({
        /// 类别 OrderResultCategoryType
        category,
        /// 变更内容 List<OrderResultCategoryItem>
        items = [],
        /// 单据影响系统数据类别名称(如仓库名，账户名)
        categorySrcName = '',
        /// 单据影响系统数据类别ID(如仓库ID，账户ID)
        categorySrcId = ''
    }) {
        this.categorySrcName = categorySrcName;
        this.categorySrcId = categorySrcId;
        this.category = category;
        this.items = items;
    }

    static fromJson(json) {
        const category = new OrderResultCategory({
            categorySrcName: Util.jsonToString(json["categorySrcName"]),
            categorySrcId: Util.jsonToString(json["categorySrcId"]),
            category: OrderResultCategoryType.fromCode(json['categoryCode']),
            items: []
        });

        if (json['items'] != null) {
            for (const itemJson of json['items']) {
                category.items.push(OrderResultCategoryItem.fromJson(itemJson));
            }
        }

        return category;
    }
}

/// 单据信息变更
export class OrderResult extends DocBase {
    constructor({
        // List<OrderResultCategory>
        orderResults = [],
        /// 商品数量 CsDouble
        commodityCount = null,
        /// 商品数量(盘点单使用) CsDouble
        commodityCount2 = null,
        /// 资金金额 CsFundDouble
        fundCount = null,
        msg = '',
        id,
        status,
        docType,
        parentDocType = null,
        parentDocCode,
        parentDocTitle,
        sourceDocType,
        sourceDocCode,
        sourceDocTitle,
        docMaker,
        operator,
        operatorId,
        title = ''
    }) {
        super({
            id,
            status,
            docType,
            parentDocType,
            parentDocCode,
            parentDocTitle,
            sourceDocType,
            sourceDocCode,
            sourceDocTitle,
            docMaker,
            operator,
            operatorId,
            title
        });

        this.commodityCount = commodityCount;
        this.commodityCount2 = commodityCount2;
        this.fundCount = fundCount;
        this.msg = msg;
        this.orderResults = orderResults;
    }

    /// 重新排序
    resort() {
        if (this.orderResults.length > 1) {
            this.orderResults.sort((a, b) => a.category.code > b.category.code ? 1 : -1);
        }
    }

    static fromJson(json) {

        let baseOrder = DocBase.fromJson(json);

        // todo commodityCount2 没有初始化，盘点单的时候查看
        const orderResult = new OrderResult({
            commodityCount: json['commodityCount'] ?
                Util.jsonToCsDouble(json['commodityCount'], 0) : null,
            fundCount: json['fundCount'] ?
                Util.jsonToCsFundDouble(json['fundCount'], 0) : null,
            msg: Util.jsonToString(json['msg']),
            orderResults: []
        });

        Object.assign(orderResult, baseOrder);


        if (json['orderResults'] != null) {
            for (const item of json['orderResults']) {
                orderResult.orderResults.push(OrderResultCategory.fromJson(item));
            }
        }
        return orderResult;
    }
}